package com.gblw.rjkf.pj.service.impl;

import cn.dev33.satoken.stp.StpUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gblw.rjkf.pj.entity.SysDictDetails;
import com.gblw.rjkf.pj.mapper.SysDictDetailsMapper;
import com.gblw.rjkf.pj.service.SysDictDetailsService;
import com.gblw.rjkf.pj.utils.Result;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * SysDictDetailsServiceImpl(SysDictDetails)
 *
 * @author wj
 * @since 2024-09-16 17:36:43
 */
@Slf4j
@Service
public class SysDictDetailsServiceImpl extends ServiceImpl<SysDictDetailsMapper, SysDictDetails> implements SysDictDetailsService {

    @Override
    public Result page(Integer pageNum, Integer pageSize, String dictId, String name) {
        QueryWrapper<SysDictDetails> queryWrapper = new QueryWrapper<>();
        if (StringUtils.isNotBlank(name)) {
            queryWrapper.eq("name", name);
        }
        if (StringUtils.isNotBlank(dictId)) {
            queryWrapper.eq("dict_id", dictId);
        }
        Page<SysDictDetails> page = new Page<>(pageNum, pageSize);
        IPage<SysDictDetails> pageList = baseMapper.selectPage(page, queryWrapper);
        return Result.success("查询成功", pageList);
    }

    @Override
    public Result insert(SysDictDetails sysDictDetails) {
        sysDictDetails.setUid(Integer.valueOf(StpUtil.getLoginId().toString()));

        // 使用 QueryWrapper 构造查询条件
        QueryWrapper<SysDictDetails> queryWrapper = new QueryWrapper<>();
        queryWrapper.or(orWrapper -> {
            orWrapper.eq("name", sysDictDetails.getName())
                    .or()
                    .eq("code", sysDictDetails.getCode());
        }).eq("dict_id", sysDictDetails.getDictId());

        // 查询是否存在重复的 name 或 values
        List<SysDictDetails> list = baseMapper.selectList(queryWrapper);

        if (!list.isEmpty()) {
            return Result.error("字典已存在，请检查");
        } else {
            // 插入新记录
            baseMapper.insert(sysDictDetails);
            return Result.success("添加成功", "");
        }
    }

    @Override
    public Result update(SysDictDetails sysDictDetails) {
        // 设置更新者的 UID
        sysDictDetails.setUid(Integer.valueOf(StpUtil.getLoginId().toString()));

        // 获取当前要更新的记录的 ID
        Integer currentId = sysDictDetails.getId();

        // 创建查询包装器
        QueryWrapper<SysDictDetails> queryWrapper = new QueryWrapper<>();

        // 构建 OR 条件：name 或 code 任意一个存在，并且排除当前记录的 ID
        queryWrapper.or(orWrapper -> {
            orWrapper.eq("name", sysDictDetails.getName());
            orWrapper.eq("code", sysDictDetails.getCode());
        }).ne(currentId != null, "id", currentId).ne("dict_id", sysDictDetails.getDictId());

        // 执行查询
        List<SysDictDetails> list = baseMapper.selectList(queryWrapper);

        // 检查是否存在
        if (!list.isEmpty()) {
            return Result.error("字典已存在，请检查");
        } else {
            // 更新记录
            baseMapper.updateById(sysDictDetails);
            return Result.success("更新成功", "");
        }
    }

    @Override
    public Result getBydictId(String dictCode) {
        QueryWrapper<SysDictDetails> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("dict_code", dictCode);
        List<SysDictDetails> list = baseMapper.selectList(queryWrapper);
        if (list.isEmpty()) {
            return Result.error("暂无数据");
        }else{
            List<Map<String,Object>> mapList = new ArrayList<>();
            for (SysDictDetails sysDictDetails : list) {
                Map<String, Object> result = new HashMap<>();
                result.put("label", sysDictDetails.getName()); // 假设 getName() 返回 dictLabel
                result.put("value", sysDictDetails.getCode()); // 假设 getCode() 返回 dictValue
                mapList.add(result);
            }
            return Result.success("查询成功",mapList);
        }

    }
}
